﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;

namespace FDeal.WUC
{
    public partial class DangNhap : System.Web.UI.UserControl
    {
        public static THONGTINTAIKHOAN current = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["isLogin"] == null || (bool)Session["isLogin"] == false)
                    SetDangNhap(false);
                else
                    SetDangNhap(true);
            }
        }

        protected void Login_Click(object sender, EventArgs e)
        {            
            ThongTinTaiKhoanBUS taikhoanBUS = new ThongTinTaiKhoanBUS();
            THONGTINTAIKHOAN taikhoan = taikhoanBUS.KiemTraDangNhap(User.Text, Password.Text);
            if (taikhoan == null)
            {
                lbError.Text = "Đăng nhập sai";
            }
            else
            {
                Session["isLogin"] = true;
                Session["nguoiDung"] = taikhoan;

                current = taikhoan;


                if (taikhoan.LOAITAIKHOAN == 2)
                {
                    Response.Redirect("~/PH_Admin/ThanhVien.aspx");
                }

                if (taikhoan.LOAITAIKHOAN == 1)
                {
                    Response.Redirect("~/PH_DoanhNghiep/PHDN_Default.aspx");
                }

                Response.Redirect(Request.Url.ToString());
            }
        }

        private void SetDangNhap(bool p)
        {
            PanelDangNhap.Visible = !p;
            PanelMenu.Visible = p;
            if (p)
            {
                lbError.Text = "";
                lbTen.Text = ((THONGTINTAIKHOAN)Session["nguoiDung"]).TENDANGNHAP;
            }
            else
            {
                lbError.Text = "";
                lbTen.Text = "";
            }
        }
        

        protected void Register_Click(object sender, EventArgs e)
        {
            Session["URLHienTai"] = Request.Url.ToString();
            Response.Redirect("~/DangKyTaiKhoan.aspx");
        }
        
        protected void DangXuat_Click(object sender, EventArgs e)
        {
            Session["isLogin"] = false;
            Session["nguoiDung"] = null;

            Response.Redirect("~/Default.aspx");
        }
    }
}